﻿namespace AnyOffice.WebControls
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [DefaultProperty("Text"), ToolboxData("<{0}:HFMenu runat=server></{0}:HFMenu>")]
    public class HFMenu : Menu
    {
        public void BindWebMenu(DataView dv, string strFirCode, string strFirText, string strSecCode, string strSecText)
        {
            this.BindWebMenu(dv, strFirCode, strFirText, "", "", strSecCode, strSecText, "", "");
        }

        public void BindWebMenu(DataView dv, string strCode, string strText, string strImage, string strUrl, string strTargetFrame)
        {
            if (this.ValidePara(strCode, strText))
            {
                for (int i = 0; i < dv.Count; i++)
                {
                    MenuItem child = new MenuItem();
                    child.Value = dv[i][strCode].ToString().Trim();
                    child.Text = dv[i][strText].ToString().Trim();
                    if ((strImage != null) && (strImage != ""))
                    {
                        child.ImageUrl = dv[i][strImage].ToString().Trim();
                    }
                    if ((strUrl != null) && (strUrl != ""))
                    {
                        child.NavigateUrl = dv[i][strUrl].ToString().Trim();
                    }
                    if ((strTargetFrame != null) && (strTargetFrame != ""))
                    {
                        child.Target = strTargetFrame;
                    }
                    base.Items.Add(child);
                }
            }
        }

        public void BindWebMenu(MenuItem item, DataView dv, string strFirCode, string strFirText, string strSecCode, string strSecText)
        {
            this.BindWebMenu(item, dv, strFirCode, strFirText, "", "", strSecCode, strSecText, "", "");
        }

        public void BindWebMenu(DataView dv, string strFirCode, string strFirText, string strFirImage, string strSecCode, string strSecText, string strSecImage)
        {
            this.BindWebMenu(dv, strFirCode, strFirText, strFirImage, "", strSecCode, strSecText, strSecImage, "");
        }

        public void BindWebMenu(MenuItem item, DataView dv, string strCode, string strText, string strImage, string strUrl, string strTargetFrame)
        {
            if (this.ValidePara(strCode, strText))
            {
                for (int i = 0; i < dv.Count; i++)
                {
                    MenuItem child = new MenuItem();
                    child.Value = dv[i][strCode].ToString().Trim();
                    child.Text = dv[i][strText].ToString().Trim();
                    if ((strImage != null) && (strImage != ""))
                    {
                        child.ImageUrl = dv[i][strImage].ToString().Trim();
                    }
                    if ((strUrl != null) && (strUrl != ""))
                    {
                        child.NavigateUrl = dv[i][strUrl].ToString().Trim();
                    }
                    if ((strTargetFrame != null) && (strTargetFrame != ""))
                    {
                        child.Target = strTargetFrame;
                    }
                    item.ChildItems.Add(child);
                }
            }
        }

        public void BindWebMenu(MenuItem item, DataView dv, string strFirCode, string strFirText, string strFirImage, string strSecCode, string strSecText, string strSecImage)
        {
            this.BindWebMenu(item, dv, strFirCode, strFirText, strFirImage, "", strSecCode, strSecText, strSecImage, "");
        }

        public void BindWebMenu(DataView dv, string strFirCode, string strFirText, string strFirImage, string strFirUrl, string strSecCode, string strSecText, string strSecImage, string strSecUrl)
        {
            if (this.ValidePara(strFirCode, strFirText, strSecCode, strSecText))
            {
                base.Items.Clear();
                string strB = null;
                int num = -1;
                for (int i = 0; i < dv.Count; i++)
                {
                    string str2 = dv[i][strFirCode].ToString().Trim();
                    if (str2.CompareTo(strB) != 0)
                    {
                        strB = str2;
                        MenuItem item = new MenuItem();
                        item.Value = dv[i][strFirCode].ToString().Trim();
                        item.Text = dv[i][strFirText].ToString().Trim();
                        if ((strFirImage != null) && (strFirImage != ""))
                        {
                            item.ImageUrl = dv[i][strFirImage].ToString().Trim();
                        }
                        if ((strFirUrl != null) && (strFirUrl != ""))
                        {
                            item.NavigateUrl = dv[i][strFirUrl].ToString().Trim();
                        }
                        base.Items.Add(item);
                        num++;
                    }
                    MenuItem child = new MenuItem();
                    child.Value = dv[i][strSecCode].ToString().Trim();
                    child.Text = dv[i][strSecText].ToString().Trim();
                    if ((strSecImage != null) && (strSecImage != ""))
                    {
                        child.ImageUrl = dv[i][strSecImage].ToString().Trim();
                    }
                    if ((strSecUrl != null) && (strSecUrl != ""))
                    {
                        child.NavigateUrl = dv[i][strSecUrl].ToString().Trim();
                    }
                    base.Items[num].ChildItems.Add(child);
                }
            }
        }

        public void BindWebMenu(MenuItem item, DataView dv, string strFirCode, string strFirText, string strFirImage, string strFirUrl, string strSecCode, string strSecText, string strSecImage, string strSecUrl)
        {
            if (this.ValidePara(strFirCode, strFirText, strSecCode, strSecText))
            {
                string strB = null;
                int num = -1;
                for (int i = 0; i < dv.Count; i++)
                {
                    string str2 = dv[i][strFirCode].ToString().Trim();
                    if (str2.CompareTo(strB) != 0)
                    {
                        strB = str2;
                        MenuItem item2 = new MenuItem();
                        item2.Value = dv[i][strFirCode].ToString().Trim();
                        item2.Text = dv[i][strFirText].ToString().Trim();
                        if ((strFirImage != null) && (strFirImage != ""))
                        {
                            item2.ImageUrl = dv[i][strFirImage].ToString().Trim();
                        }
                        if ((strFirUrl != null) && (strFirUrl != ""))
                        {
                            item2.NavigateUrl = dv[i][strFirUrl].ToString().Trim();
                        }
                        item.ChildItems.Add(item2);
                        num++;
                    }
                    MenuItem child = new MenuItem();
                    child.Value = dv[i][strSecCode].ToString().Trim();
                    child.Text = dv[i][strSecText].ToString().Trim();
                    if ((strSecImage != null) && (strSecImage != ""))
                    {
                        child.ImageUrl = dv[i][strSecImage].ToString().Trim();
                    }
                    if ((strSecUrl != null) && (strSecUrl != ""))
                    {
                        child.NavigateUrl = dv[i][strSecUrl].ToString().Trim();
                    }
                    item.ChildItems[num].ChildItems.Add(child);
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.CssClass = "menubg";
            base.DynamicHoverStyle.CssClass = "menuhover";
            base.DynamicMenuItemStyle.CssClass = "menuitem";
        }

        private bool ValidePara(string strCode, string strText)
        {
            if ((((strCode == null) || (strText == null)) || (strCode == "")) || (strText == ""))
            {
                this.Parent.Page.Response.Write("<script language='javascript'>alert('strCode或strText参数至少有一个为空！')</script>");
                return false;
            }
            return true;
        }

        private bool ValidePara(string strFirCode, string strFirText, string strSecCode, string strSecText)
        {
            if (((((strFirCode == null) || (strFirText == null)) || ((strSecCode == null) || (strSecText == null))) || (((strFirCode == "") || (strFirText == "")) || (strSecCode == ""))) || (strSecText == ""))
            {
                this.Parent.Page.Response.Write("<script language='javascript'>alert('strFirCode、strFirText、strSecCode、strSecText参数至少有一个为空！')</script>");
                return false;
            }
            return true;
        }
    }
}

